<html>
	<head>
		<title>
			Mind Class Generator
		</title>
		<style type='text/css'>
			input[type=text]
			{
				width:120px;
				font-style:italic;
			}
			body, td, li
			{
				font-family: Tahoma, Sans-Serif;
				font-size:12px;
				color: #333;
			}
		</style>
		<script>
			window.onload= function(){
				document.getElementById('fName').focus();
				think();
			}
			var ucFirst= function(o){
				return o.substring(0, 1).toUpperCase() + o.substring(1, o.length);
			}
			function think()
			{
				var fName= document.getElementById('fName').value || '???';
				
				var attrList= document.getElementById('attList').getElementsByTagName('TR');
				
				//var atts= document.getElementById('atts').value.replace(/ /g, '').split(',');
				var sepGetSet= document.getElementById('sepGetSet').checked;
				var withComments= document.getElementById('withComments').checked;
				var str= '';
				var ext= document.getElementById('fExtends').value;
				var imp= document.getElementById('fImplements').value;
				
				if(withComments)
				{
					str+= "/**\n";
					str+= "* Generate by PHPDevBar, Add-on for FireFox\n";
					str+= "* PHP Class "+ucFirst(fName)+"\n";
					str+= "* @author	Felipe Nascimento <felipenmoura@gmail.com>\n";
					str+= "* @link  https://addons.mozilla.org/en-US/firefox/addon/12686\n";
					// version refered to the mind_class_generator file
					str+= "* @version  2.0\n";
					str+= "*/\n";
				}
				str+= (document.getElementById('fStaticClass').checked? 'static ': '') + "class "+ucFirst(fName);
				str+= ext.replace(/ /g, '')==''? '': ' extends '+ext;
				str+= imp.replace(/ /g, '')==''? '': ' implements '+imp;
				str+= '\n';
				str+= "{\n";
				var strFunc= '';
				var curAtt= null;
				var html = "<form action='<?php echo $_SERVER['PHP_SELF']; ?>'\n      method='POST'>\n";
				html+= "\t<table>";
				var c= 1;
				var formRows= 0;
				var attrNameList= Array();
				if(document.getElementById('noAttrYet').style.display == 'none')
					for(var i=0, j=attrList.length; i<j; i++)
					{
						if(c==1)
						{
							html+= "\n\t\t<tr>";
							formRows++;
						}
						curAtt= {
									name: attrList[i].getElementsByTagName('TD')[0].innerHTML,
									visibility: attrList[i].getElementsByTagName('TD')[1].innerHTML,
									static: (attrList[i].getElementsByTagName('TD')[2].innerHTML=='Yes'? true: false),
									comment: attrList[i].getAttribute('comment')
								}
						attrNameList.push(curAtt.name);
						html+= "\n\t\t\t<td class='formLabel'>\n\t\t\t\t"+ucFirst((curAtt.comment.replace(/ /g, '')=='')?curAtt.name: curAtt.comment)+'\n\t\t\t</td>';
						var cS= ((document.getElementById('formColumNumber').value*2) - c*2 +1);
						if(formRows>1 && i==j-1 && c<document.getElementById('formColumNumber').value)
							html+= "\n\t\t\t<td class='formInput' colspan='"+cS+"'>";
						else
							html+= "\n\t\t\t<td class='formInput'>";
						html+= "\n\t\t\t\t<input type='text' name='"+curAtt.name+"'/>";
						html+= "\n\t\t\t</td>";
						if(c == document.getElementById('formColumNumber').value || i==j-1)
						{
							html+= "\n\t\t</tr>";
							c=0;
						}
						c++;
						str+= "\t"+(curAtt.visibility||'public')+
							  (curAtt.static? ' static':'')+
							  " $"+curAtt.name+"; "+ ((curAtt.comment.replace(/ /g, '') == '')? '': '// '+curAtt.comment)+'\n';
						if(sepGetSet)
						{
							if(withComments)
							{
								strFunc+= "\n\t/**\n";
								strFunc+= "\t* Public function set"+ucFirst(curAtt.name)+"\n";
								strFunc+= "\t* @return void\n";
								strFunc+= "\t* @param  mixed $val\n";
								strFunc+= "\t*/\n";
							}
							strFunc+= "\tpublic function set"+ucFirst(curAtt.name)+'($val="")\n';
							strFunc+= "\t{\n";
							strFunc+= "\t\t$this->"+curAtt.name+"= $val;\n";
							strFunc+= "\t}\n";
							if(withComments)
							{
								strFunc+= "\n\t/**\n";
								strFunc+= "\t* Public function get"+ucFirst(curAtt.name)+"\n";
								strFunc+= "\t* @return "+curAtt.name+"\n";
								strFunc+= "\t*/\n";
							}
							strFunc+= "\tpublic function get"+ucFirst(curAtt.name)+'()\n';
							strFunc+= "\t{\n";
							strFunc+= "\t\treturn $this->"+curAtt.name+";\n";
							strFunc+= "\t}\n";
						}else{
								if(withComments)
								{
									strFunc+= "\n\t/**\n";
									strFunc+= "\t* Public function "+curAtt.name+"\n";
									strFunc+= "\t* @return "+curAtt.name+"|void\n";
									strFunc+= "\t* @param  mixed|void $val\n";
									strFunc+= "\t*/\n";
								}
								strFunc+= "\tpublic function "+curAtt.name+"($val=false)\n";
								strFunc+= "\t{\n";
								strFunc+= "\t\tif($val)\n\t\t\t$this->"+curAtt.name+"= $val;\n";
								strFunc+= "\t\telse\n\t\t\treturn $this->"+curAtt.name+";\n";
								strFunc+= "\t}\n";
							 }
					}
				str+= strFunc;
				if(document.getElementById('withConstructor').checked)
				{
					var ps= document.getElementById('constructorsParams').value;
					str+= "\n\t/* constructor method */";
					if(ps == 'Object')
						str+= "\n\tpublic function __construct($o= false)";
					else if(ps == 'Array')
							str+= "\n\tpublic function __construct($ar= false)";
						 else{
						 		if(attrNameList.length > 1)
							 		str+= "\n\tpublic function __construct($"+attrNameList.join('=false, $')+"=false)";
							 	else
							 		str+= "\n\tpublic function __construct($"+attrNameList[0]+"=false)";
							 }
					str+= "\n\t{";
					var valor= null;
					for(var i=0, j= attrNameList.length; i<j; i++)
					{
						valor= ps== 'Object'? "isset($o->"+ attrNameList[i] +")? $o->" +attrNameList[i]+": false":
 											   ps=='Array'? "isset($ar['"+attrNameList[i]+"'])? $ar['"+attrNameList[i]+"']: false":
									 			  		    "$"+attrNameList[i];
						
						str+= "\n\t\t$this->"+attrNameList[i]+'= '+ valor+';';
					}
					str+= "\n\t\t//add your code here";
					str+= "\n\t}\n";
				}
				str+= "}\n";
				if(showingCurrently == 'class')
					document.getElementById('code').value= str;
				else{
						if(document.getElementById('formColumNumber').value < attrList.length)
							cS= (document.getElementById('formColumNumber').value*2);
						else
							cS= attrList.length*2;
						document.getElementById('code').value = html+'\n\t';
						document.getElementById('code').value+= "\t<tr>\n\t\t\t<td colspan='"+cS+"'>\n\t\t\t\t<center>\n\t\t\t\t\t<input type='submit' value='Send'/>\n\t\t\t\t</center>\n\t\t\t</td>\n\t\t</tr>\n\t"
						document.getElementById('code').value+= '</table>\n</form>';
					}
			}
			
			function removeAtt(o)
			{
				if(o.parentNode.parentNode.parentNode.getElementsByTagName('TR').length == 1)
					document.getElementById('noAttrYet').style.display= '';
				o.parentNode.parentNode.parentNode.removeChild(o.parentNode.parentNode);
				think();
			}
			
			function addAttribute()
			{
				var attName, vis, sta= false;
				attName= document.getElementById('attr').value;
				vis= document.getElementById('attVisibility').value;
				sta= document.getElementById('fStaticAtt').checked;
				var tr= document.createElement('TR');
				var td1= document.createElement('TD');
				var td2= document.createElement('TD');
				var td3= document.createElement('TD');
				var td4= document.createElement('TD');
				td1.innerHTML= attName;
				td2.innerHTML= vis;
				td3.innerHTML= sta? 'Yes': 'No';
				td4.innerHTML= "<input type='image' title='Remove this attribute' alt='Remove this attribute' src='imgs/remove.png' onclick='removeAtt(this)'/>";
				tr.style.height= '24px';
				tr.setAttribute('comment', document.getElementById('attrComment').value);
				document.getElementById('attrComment').value= '';
				tr.appendChild(td1);
				tr.appendChild(td2);
				tr.appendChild(td3);
				tr.appendChild(td4);
				document.getElementById('attList').appendChild(tr);
				document.getElementById('attr').focus();
				document.getElementById('attr').select();
				document.getElementById('noAttrYet').style.display= 'none';
				think();
			}
			var showingCurrently= 'class';
			function toggle()
			{
				var t1= document.getElementById('tabClass');
				var t2= document.getElementById('tabForm');
				t1.className= t1.className== 'activeTab'? 'inactiveTab': 'activeTab';
				t2.className= t2.className== 'activeTab'? 'inactiveTab': 'activeTab';
				showingCurrently= t1.className== 'activeTab'? 'html': 'class';
				think();
			}
		</script>
		<style type='text/css'>
			.attsGrid
			{
				border-left: solid 1px #666;
				border-top: solid 1px #666;
				border-right: solid 1px #aaa;
				border-bottom: solid 1px #aaa;
				width:200px;
				height:100%;
				-moz-border-radius:6px 6px 0px 0px;
			}
			.attsGrid tr td
			{
				border-left: solid 1px #aaa;
				border-top: solid 1px #aaa;
				text-align:center;
				font-weight:bold;
				color:#444;
			}
			.attsGrid #attList tr td input
			{
				height:14px;
			}
			.attsGrid #attList tr td
			{
				background-color:#fff;
				text-align:left;
				font-weight:normal;
				border:none;
				border-left: solid 1px #aaa;
				border-top: solid 1px #aaa;
			}
			.activeTab
			{
				border-bottom:solid 2px #aaa;
				border-left:solid 2px #aaa;
				border-right:solid 1px #aaa;
				border-top:solid 1px #aaa;
				padding-left: 4px;
				padding-right: 4px;
				font-weight:bold;
				cursor:pointer;
				background-color:#fff;
				color:#555;
			}
			.inactiveTab
			{
				border-bottom:solid 2px #aaa;
				border-left:solid 2px #aaa;
				font-weight:bold;
				border-right:solid 1px #aaa;
				border-top:solid 1px #aaa;
				padding-left: 4px;
				padding-right: 4px;
				cursor:default;
				background-color:#fff;
				color:#aaa;
			}
		</style>
	</head>
	<body bgcolor="#e6e6df">
		<table style="width:100%;height:100%;">
			<tr style='height:22px;'>
				<td style='width:75px;'>
					Class Name
				</td>
				<td>
					<input type='text'
						   id='fName'
						   onkeyup='think();'>
					<input type='checkbox'
						   id='fStaticClass'
						   onclick="think();"/> Static
					<input type='checkbox'
						   id='sepGetSet'
						   onclick='think()'>
					Separated Get and Set
				</td>
				<td rowspan='8'
					style='vertical-align:top;width:200px;'>
					<table class='attsGrid' cellpadding='2' cellspacing='0'>
						<tr style='height:20px;'>
							<td>
								Property
							</td>
							<td>
								Visibility
							</td>
							<td>
								Static
							</td>
							<td>
								<br/>
							</td>
						</tr>
						<tbody id='attList'>
						</tbody>
						<tr id='noAttrYet'>
							<td colspan='4'
								style='height:60px;font-weight:bold;background-color:#fff;'>
								<center>
									No properties yet
								</center>
							</td>
						</tr>
						<tr>
							<td style='background-color:#fff;' colspan='4'>
								<br/>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr style='height:22px; width: 250px;'>
				<td>
					Extends
				</td>
				<td>
					<input id='fExtends' type='text' onkeyup='think();'/>&nbsp;
					Implements <input id='fImplements' type='text' onkeyup='think();'/>
				</td>
			</tr>
			<tr style='height:22px; width: 250px;'>
				<td>
					Attributes
				</td>
				<td>
					<nobr>
						<input type='text'
							   id='attr'
							   style='width:120px;'
							   onfocus="this.select();"
							   onkeyup="if(event.keyCode == 13) addAttribute();">
						<select id='attVisibility'>
							<option value='public'>
								public
							</option>
							<option value='private'>
								private
							</option>
							<option value='protected'>
								protected
							</option>
						</select>
						<input type='checkbox'
							   id='fStaticAtt'/> Static
						Comment
						<input id='attrComment'
							   type='text'
							   onkeyup="if(event.keyCode == 13) addAttribute();"/>
						<input  type='button'
								value=' + '
								onclick="addAttribute()"/>
					</nobr>
				</td>
			</tr>
			<tr style='height:22px;'>
				<td colspan='2'>
					<input type='checkbox'
						   id='withComments'
						   checked='checked'
						   onclick='think()'>
					With Comments
					&nbsp;&nbsp;&nbsp;
					<input type='checkbox'
						   id='withConstructor'
						   checked='checked'
						   onclick='think(); if(this.checked)document.getElementById("constructorsParams").removeAttribute("disabled"); else document.getElementById("constructorsParams").disabled="disabled";'>
					With Constructor 
					(Params: 
					<select id='constructorsParams'
							onchange="think();">
						<option value='Array'>Array</option>
						<option value='Object'>Object</option>
						<option value='Sorted'>Sorted</option>
					</select>
					)
					&nbsp;&nbsp;&nbsp;
					Form columns:
					<select id='formColumNumber'
							onchange="think();">
						<option value='1'>
							1
						</option>
						<option value='2' selected='selected'>
							2
						</option>
						<option value='3'>
							3
						</option>
						<option value='4'>
							4
						</option>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan='2'>
					<textarea id="code"
							  readonly='readonly'
							  style='background-color:#fff;
									 color:#333;
									 width:100%;
									 height:100%;
									 white-space:nowrap;'></textarea>
				</td>
			</tr>
			<tr>
				<td style='height:26px;padding-left:15px;' colspan='2'>
					<table width='100%'>
						<tr>
							<td>
								<table style='margin-top:0px; padding-top:0px;'>
									<tr>
										<td class='inactiveTab'
											id='tabClass'
											onclick="toggle()">
											Class
										</td>
										<td class='activeTab'
											id='tabForm'
											onclick="toggle()">
											<nobr>HTML Form</nobr>
										</td>
									</tr>
								</table>
							</td>
							<td style='text-align:right;'>
								Check <b><i><a target='_quot' href='http://thewebmind.org' >theWebMind</a></i></b> out, to have more options
							</td>
					</tr>
				</table>
			</tr>
		</table>
	</body>
</html>
